﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twipler.App;
using Twipler.Data.Access;
using Twipler.Data.Entity.Theme;
using Twipler.Data.Entity.Twipler;
using Twipler.WebApp;

using NHibernate.Criterion;
using System.Web.UI;

namespace Settings.Theme
{
    [QueryStringHash]
    [RequireUser(VisitorRole.Registered)]
    public partial class SelectStyle : System.Web.UI.Page
    {
        private Design Design;

        protected void Page_Load(object sender, EventArgs e)
        {
            ProcessRequest();

            int designId;
            if (int.TryParse(Request.QueryString["designid"], out designId) == false)
                Response.Redirect("select.aspx"); 

            Design = Factory.Design.GetById(designId, false);
            if (Design == null)
                Response.Redirect("select.aspx");

            if (IsPostBack)
                return;

            List<Twipler.Data.Entity.Theme.Style> styles = new List<Twipler.Data.Entity.Theme.Style>();

            if (!Current.Visitor.HasRole(VisitorRole.StyleCreator))
            {
                pnlCreateNew.Visible = false;
            }
            else
            {
                styles.Add(new Style() { Name = "Blank", Id = 0 });
                styles.AddRange(Factory.Style.GetByCriteria(
                    Expression.And(
                        Expression.Eq("ParentDesign", Design),
                        Expression.Eq("State", (int)StyleState.Published))
                        ));

                drpList.DataSource = styles;
                drpList.DataTextField = "Name";
                drpList.DataValueField = "Id";
                drpList.DataBind();
            }

            StringBuilder html = new StringBuilder();

            
            styles = Factory.Style.GetByCriteria(
                        Expression.And(
                            Expression.Eq("ParentDesign", Design),
                            Expression.Eq("State", (int)StyleState.Published))
                            ) ;

            int totalStyles = styles.Count();

            if (styles.Count() > 0)
            {
                html.Append("<h3>Published</h3>");
                ListStyles(html, styles);
            }
            
            styles = Factory.Style.GetByCriteria(
                Expression.And(
                        Expression.Eq("State", (int)DesignState.Private),
                    Expression.And(
                        Expression.Eq("ParentDesign", Design),
                        Expression.Eq("CreatedBy", Current.Visitor)
                        )));

            totalStyles += styles.Count();

            if (styles.Count() > 0)
            {
                html.Append("<h3>Your private styles</h3>");
                ListStyles(html, styles);
            }


            // No styles and can't create style, redirect home
            if ((totalStyles == 0) && !Current.Visitor.HasRole(VisitorRole.StyleCreator))
                Response.Redirect("../../default.aspx");

            litStuff.Text = html.ToString();

        }

        private void ListStyles(StringBuilder html, List<Style> styles)
        {
            foreach (Style style in styles)
            {
                html.AppendFormat("<div style=\"padding: 5px;\"><A href=\"{0}\">{1}</a></div>",
                                   QueryStringHash.Protect(string.Format("selectstyle.aspx?setstyle={0}",style.Id)), style.Name);
            }
        }

        private void ProcessRequest()
        {
            if (Request.QueryString["setstyle"] != null)
            {
                int styleId;
                if (int.TryParse(Request.QueryString["setstyle"], out styleId))
                {
                    Style selected = Factory.Style.GetById(styleId, false);
                    if (selected != null)
                    {
                        Current.Visitor.Style = selected;
                        Factory.Visitor.SaveOrUpdate(Current.Visitor);

                        Response.Redirect("../../default.aspx");
                    }
                }
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Style newStyle;

            if (drpList.SelectedValue != "0")
            {
                Style copy = Factory.Style.GetById(int.Parse(drpList.SelectedValue), false);
                newStyle= copy.Clone();
            }
            else
            {
                newStyle = new Style();
            }

            Factory.Style.SaveOrUpdate(newStyle);
            Response.Redirect(QueryStringHash.Protect("stylelist.aspx?edit=" + newStyle.Id));
        
        }
    }
}